﻿using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class MyBookController : BaseController
    {
        // GET: Admin/Book
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = BookService.GetAll().Where(a => a.IsAbort == false);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.Name.Contains(key) || a.ISBN.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        public ActionResult Detail()
        {
            var userId = Query.Value("Id").ToInt();
            var order = new Books();
            if (userId > 0)
            {
                order = BookService.GetAll().FirstOrDefault(a => a.Id == userId);
            }
            return View(order);
        }

        public ActionResult EditGitNum()
        {
            var type = Query.Value("type").ToInt();

            var time = BaseService<BookGits>.GetAll().FirstOrDefault(a=>a.BookId == type);
            return View(time);
        }

        [HttpPost]
        public ActionResult EditGitNum(int? z)
        {
            var type = Query.Value("type").ToInt();
            var deadtime = int.Parse(Query.Value("deadtime"));
            //var url = Query.Value("url");
            var time = BaseService<BookGits>.GetAll().FirstOrDefault(a=>a.BookId == type);
            time.Remain = deadtime;
            BaseService<BookGits>.Change(time);
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/mybook/EditGitNum/?type=" + type);
        }

        /// <summary>
        /// 新增和修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            var typeId = Query.Value("typeId").ToInt();
            var companytype = new Books();
            if (typeId > 0)
            {
                companytype = BookService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(companytype);
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="z"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(int? z)
        {
            var name = Query.Value("name");
            var typeId = Query.Value("typeId").ToInt();
            var author = Query.Value("author");
            var description = Query.Value("description");
            var coverimg = Query.Value("coverimg");
            var ISBN = Query.Value("ISBN");
            var OriginPrice = Query.Value("originprice").ToDecimal();
            var OrderPrice = Query.Value("orderprice").ToDecimal();
            var type = Query.Value("type").ToInt();
            var py = Query.Value("py");
            var companytype = new Books();
            if (typeId > 0)
            {
                companytype = BookService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (companytype != null)
                {
                    companytype.Name = name;
                    companytype.Author = author;
                    companytype.Description = description;
                    companytype.CoverImg = coverimg;
                    companytype.ISBN = ISBN;
                    companytype.BookTypeId = type;
                    companytype.OriginPrice = OriginPrice;
                    companytype.OrderPrice = OrderPrice;
                    companytype.JPY = StringService.HanToPinyin(name);
                    companytype.PY = py;
                    BookService.Change(companytype);
                }
            }
            else
            {
                companytype.Name = name;
                companytype.Author = author;
                companytype.Description = description;
                companytype.CoverImg = coverimg;
                companytype.IsAbort = false;
                companytype.CreateTime = DateTime.Now;
                companytype.ISBN = ISBN;
                companytype.BookTypeId = type;
                companytype.OriginPrice = OriginPrice;
                companytype.OrderPrice = OrderPrice;
                companytype.JPY = StringService.HanToPinyin(name);
                companytype.PY = py;
                BookService.Add(companytype);
                var id = BookService.GetAll().FirstOrDefault(a => a.ISBN == ISBN).Id;
                BaseService<BookGits>.Add(new BookGits
                {
                    BookId = id,
                    Remain = 10
                });
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/mybook/edit?typeId=" + typeId);
        }
        [HttpPost]
        public JsonResult DownBook()
        {
            var bid = Query.Value("bid").ToInt();
            var book = BookService.GetAll().Find(bid);
            book.IsAbort = true;
            BookService.Change(book);
            return Json(new { success = "下架成功！" }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult BookType()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var booktypes = BookTypeService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                booktypes = booktypes.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = booktypes.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        public ActionResult EditType()
        {
            var typeId = Query.Value("type").ToInt();
            var companytype = new BookTypes();
            if (typeId > 0)
            {
                companytype = BaseService<BookTypes>.GetAll().FirstOrDefault(a => a.Id == typeId);
            }

            return View(companytype);
        }

        [HttpPost]
        public ActionResult EditType(int? z)
        {
            var type = Query.Value("type").ToInt();
            var deadtime = Query.Value("deadtime");
            var time = new BookTypes();
            if (type > 0)
            {
                time = BaseService<BookTypes>.GetAll().FirstOrDefault(a => a.Id == type);
                if (time != null)
                {
                    time.Name = deadtime;
                    BaseService<BookTypes>.Change(time);
                }

            }
            else
            {
                time.Name = deadtime;
                time.IsAbort = false;
                time.CreateTime = DateTime.Now;
                BaseService<BookTypes>.Add(time);
            }
           
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/mybook/EditType/?type=" + type);
        }

        [HttpPost]
        public JsonResult DeleteType()
        {
            var bid = Query.Value("tid").ToInt();
            var books = BookService.GetAll().Where(a => a.BookTypeId == bid);
            if (books.Count() > 0)
            {
                return Json(new { success = "失败！请先移除该分类下所有图书！" }, JsonRequestBehavior.AllowGet);
            }
            var book = BookTypeService.GetAll().Find(bid);
            SysInfo.DB.BookTypes.Remove(book);
            SysInfo.DB.SaveChanges();
            return Json(new { success = "删除成功！" }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult DownType()
        {
            var bid = Query.Value("tid").ToInt();
            var books = BookService.GetAll().Where(a => a.BookTypeId == bid);
            foreach(var book in books)
            {
                book.IsAbort = true;
            }
            SysInfo.DB.SaveChanges();
            return Json(new { success = "该分类图书已全部下架！" }, JsonRequestBehavior.AllowGet);
        }
    }
}